<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片布局区域 -->
    <el-card>
      <!-- 搜索和添加商品区域 -->
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品表格区域 -->
      <el-table :data="goodsDataList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="120"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="100">
          <template slot-scope="scope">
              {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="400">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  created () {
    // 获取商品数据
    this.getGoodsList()
  },
  data () {
    return {
      // 新增查询参数对象
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页条目数
        pagesize: 5
      },
      // 总商品数据
      total: '',
      // 存储获取到的数据
      goodsDataList: []
    }
  },
  methods: {
    // 获取商品数据
    async getGoodsList () {
      // 请求数据
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取商品数据失败')
      this.$message.success('获取商品数据成功')
      this.goodsDataList = res.data.goods
    },
    // 当前页页面展示条目发生变化
    handleSizeChange (val) {
      // console.log(val)
      // 将修改之后的值重新赋值
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    // 监听当前页码发生改变
    handleCurrentChange (val) {
      // 将修改之后的值重新赋值
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    // 删除商品按钮的点击事件
    async removeGoods (row) {
      // 判断用户是否真的要删除
      const confirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // console.log(confirm)
      if (confirm !== 'confirm') return
      // 用户决定要删除
      // console.log(row)
      const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('删除商品失败')
      this.$message.success('删除商品成功')
      this.getGoodsList()
    },
    // 添加按钮的点击事件 -跳转添加商品页面
    addGoods () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>

</style>
